package com.alibaba.middleware.hsf.guide.serial.hessian.noclasstomap;

import java.io.File;
import java.io.FileInputStream;
import java.io.Serializable;

import org.junit.Test;

import com.taobao.hsf.com.caucho.hessian.io.AbstractHessianInput;
import com.taobao.hsf.com.caucho.hessian.io.Hessian2Input;

/**
 * <pre>
 * 验证Hessian将找不到的类型按照HashMap处理，进行反序列化
 * 
 * 本示例中，序列化了Student，后来改为Student1
 * </pre>
 * 
 * @author weipeng2k 2015年10月8日 下午4:56:43
 */
public class HessianSerial {
    
    static File file = new File("hessianserial.dat");
    //
    // @Test
    // public void serial() throws Exception {
    // Student student = new Student("weipeng", "2");
    //
    // AbstractHessianOutput out = new Hessian2Output(new FileOutputStream(file));
    // out.writeObject(student);
    // out.flush();
    // }
    
    @Test
    public void deserial() throws Exception {
        AbstractHessianInput in = new Hessian2Input(new FileInputStream(file));
        Object cl = in.readObject();
        System.out.println(cl.getClass());
        System.out.println(cl);
    }
    
    static class Student1 implements Serializable {
        
        private static final long serialVersionUID = -1665168017326327558L;
        private String            name;
        private String            pwd;
                                  
        public Student1(String name, String pwd){
            this.setName(name);
            this.setPwd(pwd);
        }
        
        public String getName() {
            return name;
        }
        
        @Override
        public String toString() {
            return "Student [name=" + name + ", pwd=" + pwd + "]";
        }
        
        public void setName(String name) {
            this.name = name;
        }
        
        public String getPwd() {
            return pwd;
        }
        
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
        
    }
}
